package group.sicher.core.utils;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;

/**
 * description:
 * author:aron
 * date:2019/03/29
 */
public class SrmUtils {

    /**
     * 下载远程文件 返回文件流InputStream
     * 注意：使用完InputStream 记得关闭流
     *
     * @param urlPath 远程文件下载路径
     * @return
     * @throws IOException
     */
    public static InputStream downloadNet(String urlPath) throws IOException {
        URL url = new URL(urlPath);
        URLConnection connection = url.openConnection();
        InputStream inputStream = connection.getInputStream();
        return inputStream;

    }

    public static InputStream downloadNetByNio(String urlPath) throws IOException {
        FileUtils.copyURLToFile(new URL(urlPath), new File("D:/ss.xls"));
        return null;
    }


    /**
     * 以流的方式下载文件
     *
     * @param path
     * @param response
     */
    public static void download(String path, HttpServletResponse response) {
        File file = new File(path);
        String filename = file.getName();
        InputStream fis = null;
        try {
            fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 设置response的Header
            setFileName(filename, response);
            //文件下载流
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(fis);
        }
    }


    /**
     * 以流的方式下载文件
     *
     * @param response
     */
    public static void downloadResponse(HttpServletResponse response, InputStream inputStream, String fileName) {
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            setFileName(fileName, response);
            while ((len = inputStream.read(b)) > 0) {
                response.getOutputStream().write(b, 0, len);
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }


    /**
     * 下载本地文件
     *
     * @param response HttpServletResponse
     * @param path     本地文件路径
     */
    public static void downLoadLcal(HttpServletResponse response, String path) {
        if (StringUtils.isBlank(path)) {
            return;
        }
        InputStream inputStream = null;
        OutputStream out = null;
        try {
            File file = new File(path);
            inputStream = new FileInputStream(file);
            String fileName = file.getName();
            setFileName(fileName, response);
            byte[] b = new byte[100];
            int len;
            out = response.getOutputStream();
            while ((len = inputStream.read(b)) > 0) {
                out.write(b, 0, len);
                out.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(out);
        }
    }

    /**
     * 下载本地文件
     *
     * @param response HttpServletResponse
     */
    public static void downLoad(HttpServletResponse response, InputStream inputStream, String fileName) {
        if (null == inputStream) {
            return;
        }
        OutputStream out = null;
        try {
            setFileName(fileName, response);
            byte[] b = new byte[100];
            int len;
            out = response.getOutputStream();
            while ((len = inputStream.read(b)) > 0) {
                out.write(b, 0, len);
                out.flush();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(inputStream);
            IOUtils.closeQuietly(out);
        }
    }


    /**
     * 文件名的定义
     * 返回response的定义
     *
     * @param fileName
     * @param response
     */
    private static void setFileName(String fileName, HttpServletResponse response) throws IOException {
        // 清空response
        response.reset();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    }


//    public static void main(String[] args) throws ParserConfigurationException, TransformerException, IOException {
////        excelToHtml(new FileInputStream("D:/p_snmp_config.xls"));
//        downloadNetByNio("http://192.168.69.77:8083/srm/59ad0364b1be5ab720bc4cf6578cb029.xlsx");
//    }


}
